<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit\Ctl;

use Srv\Apps\Proto\DTO\Skit\SkitTheaterWebAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterWebEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterWebListDTO;
use Srv\Apps\Proto\VO\Skit\SkitTheaterWebListVO;
use Srv\Apps\Proto\VO\Skit\SkitTheaterWebVO;
use Srv\Libs\Common\CommJson;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class SkitTheaterWebCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $thwfId
     * @return array
     * getSkitTheaterWebSrc
     */
    public function getSkitTheaterWebSrc(int $thwfId):array
    {
        if($thwfId < 1) return [];
        $isFail     = false;
        $info       = $this->DbAuto->getDataOne('SELECT thwf_id, thwf_name, thwf_title, thwf_sub_title, skio_ids, thwf_sort, thwf_status, thwf_create_time, thwf_update_time FROM '.$this->DbAuto->getTableNameAll('theater_web_conf').' WHERE thwf_id='.$thwfId.' AND thwf_delete=0', $isFail);
        if($isFail || !isset($info['thwf_id'])) return [];
        return $info;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return SkitTheaterWebListVO|null
     * getSkitTheaterWebList
     */
    public function getSkitTheaterWebList(Page $Page, array $whereData): ?SkitTheaterWebListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE thwf_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case SkitTheaterWebListDTO::_thwfId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thwf_id='. intval($val);
                    break;
                case SkitTheaterWebListDTO::_name:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thwf_name LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterWebListDTO::_title:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thwf_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterWebListDTO::_subTitle:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thwf_sub_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterWebListDTO::_status:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thwf_status='. intval($val);
                    break;
            }
        }
        $isFail         = false;
        $sql            = 'SELECT SQL_CALC_FOUND_ROWS thwf_id, thwf_name, thwf_title, thwf_sub_title, skio_ids, thwf_sort, thwf_status, thwf_create_time, thwf_update_time FROM '.$this->DbAuto->getTableNameAll('theater_web_conf').$whereString.' ORDER BY thwf_id DESC '.$limitString;
        $list           = $this->DbAuto->getData($sql, $isFail);
        $totalNum       = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $SkitTheaterWebVOList       = [];
        foreach($list as $info){
            $thwfIdItem             = intval($info['thwf_id']);
            if($thwfIdItem < 1) continue;
            $SkitTheaterWebVO       = $this->getSkitTheaterWebVO($info);
            if(!($SkitTheaterWebVO instanceof SkitTheaterWebVO)) continue;
            $SkitTheaterWebVOList[] = $SkitTheaterWebVO;
        }
        $SkitTheaterWebListVO       = new SkitTheaterWebListVO();
        $SkitTheaterWebListVO->setList($SkitTheaterWebVOList);
        $SkitTheaterWebListVO->setPage($Page->getPageVO($totalNum));
        return $SkitTheaterWebListVO;
    }

    /**
     * @param int $thwfId
     * @return SkitTheaterWebVO|null
     * getSkitTheaterWebInfo
     */
    public function getSkitTheaterWebInfo(int $thwfId):?SkitTheaterWebVO
    {
        $theaterWebSrc  = $this->getSkitTheaterWebSrc($thwfId);
        if(!isset($theaterWebSrc['thwf_id'])) return null;
        return $this->getSkitTheaterWebVO($theaterWebSrc);
    }

    /**
     * @param array $info
     * @return SkitTheaterWebVO|null
     * getSkitTheaterWebVO
     */
    public function getSkitTheaterWebVO(array $info):?SkitTheaterWebVO
    {
        $thwfId             = intval($info['thwf_id']??0);
        if($thwfId < 1) return null;
        $SkitTheaterWebVO   = new SkitTheaterWebVO();
        $SkitTheaterWebVO->setThwfId($thwfId);
        $SkitTheaterWebVO->setName(trim($info['thwf_name']));
        $SkitTheaterWebVO->setTitle(trim($info['thwf_title']));
        $SkitTheaterWebVO->setSubTitle(trim($info['thwf_sub_title']));
        $skioIdList         = CommJson::decodeArray($info['skio_ids']);
        $skitInfoVOListNew  = [];
        $SkitInfoVOList     = SkitInfoCtl::tag()->getSkitInfoBatch($skioIdList);
        foreach ($skioIdList as $skioIdItem){
            $SkitInfoVO     = $SkitInfoVOList[$skioIdItem]??null;
            if($SkitInfoVO !== null) $skitInfoVOListNew[] = $SkitInfoVO;
        }
        $SkitTheaterWebVO->setSkitInfoList($skitInfoVOListNew);
        $SkitTheaterWebVO->setSort(intval($info['thwf_sort']));
        $SkitTheaterWebVO->setStatus(intval($info['thwf_status']));
        $SkitTheaterWebVO->setCreateTime(intval($info['thwf_create_time']));
        $SkitTheaterWebVO->setUpdateTime(intval($info['thwf_update_time']));
        return $SkitTheaterWebVO;
    }

    /**
     * @param array $setData
     * @return int
     * skitTheaterWebAdd
     */
    public function skitTheaterWebAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitTheaterWebAddDTO::_skioIdList:
                    $skitInfoList   = SkitInfoCtl::tag()->getSkitInfoSrcBatch($val);
                    $skioIdList     = array_map('intval', array_column($skitInfoList, 'skio_id'));
                    $skioIdList     = array_values(array_intersect($val, $skioIdList));
                    $skioIdJson     = CommJson::encodeArray($skioIdList);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ids=\''.$this->DbAuto->getRealEscapeString($skioIdJson).'\'';
                    break;
                case SkitTheaterWebAddDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebAddDTO::_title:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebAddDTO::_subTitle:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_sub_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebAddDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_sort='.intval($val);
                    break;
                case SkitTheaterWebAddDTO::_status:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_status='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('theater_web_conf').' SET '.$setString.', thwf_delete=0, thwf_create_time='.$currTime.', thwf_update_time='.$currTime;
        $thwfId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $thwfId < 1) return 0;
        return $thwfId;
    }

    /**
     * @param int $thwfId
     * @param array $setData
     * @return bool
     * skitTheaterWebEdit
     */
    public function skitTheaterWebEdit(int $thwfId, array $setData):bool
    {
        if($thwfId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitTheaterWebEditDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebEditDTO::_title:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebEditDTO::_subTitle:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_sub_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterWebEditDTO::_skioIdList:
                    $skitInfoList   = SkitInfoCtl::tag()->getSkitInfoSrcBatch($val);
                    $skioIdList     = array_map('intval', array_column($skitInfoList, 'skio_id'));
                    $skioIdList     = array_values(array_intersect($val, $skioIdList));
                    $skioIdJson     = CommJson::encodeArray($skioIdList);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ids=\''.$this->DbAuto->getRealEscapeString($skioIdJson).'\'';
                    break;
                case SkitTheaterWebEditDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_sort='.intval($val);
                    break;
                case SkitTheaterWebEditDTO::_status:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thwf_status='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('theater_web_conf').' SET '.$setString.', thwf_update_time='.$currTime.' WHERE thwf_id='.$thwfId.' AND thwf_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }

    /**
     * @param int $thwfId
     * @return bool
     * skitTheaterWebDel
     */
    public function skitTheaterWebDel(int $thwfId):bool
    {
        if($thwfId < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp();
        $resNum     = $this->DbAuto->getDataNum('UPDATE '.$this->DbAuto->getTableNameAll('theater_web_conf').' SET thwf_delete=1, thwf_update_time='.$currTime.' WHERE thwf_id='.$thwfId.' AND thwf_delete=0', $isFail);
        if($isFail || $resNum < 1) return false;
        return true;
    }
}